package com.msoe.core.ride;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.rpc.IsSerializable;
import com.msoe.core.users.User;

public class Ride implements IsSerializable
{
	private int rideID;
	private int driverID;
	private long departure;
	private Location src;
	private Location dest;
	private List<Integer> passengers;
	private int numSeats;
	
	/**
	 * Default constructor for the Ride class
	 */
	public Ride()
	{
		this(-1, 0, new Location(), new Location(), 0);
	}
	
	/**
	 * Creates a new Ride
	 * @param driver The person giving the ride
	 * @param departure The time of departure
	 * @param src The source location
	 * @param dest The final destination
	 */
	public Ride(int driver, long departure, Location src, Location dest, int numSeats)
	{
		this(-1, driver, departure, src, dest, numSeats);
	}
	
	/**
	 * Creates a new Ride
	 * @param rideID The ride id
	 * @param driver The person giving the ride
	 * @param departure The time of departure
	 * @param src The source location
	 * @param dest The final destination
	 * @param numSeats The number of seats available for the ride.
	 */
	public Ride(int rideID, int driver, long departure, Location src, Location dest, int numSeats)
	{
		this.rideID = rideID;
		this.driverID = driver;
		this.departure = departure;
		this.src = src;
		this.dest = dest;
		this.numSeats = numSeats;
		
		// Initialize the passengers
		this.passengers = new ArrayList<Integer>();
	}
	
	/**
	 * Adds passengers to the ride if there are seats available
	 * @param passenger The passenger to add
	 */
	public void addPassenger(Integer... passenger)
	{
		if(this.passengers.size() + passenger.length > this.numSeats) throw new RideException("Tried to add a passenger, but the ride already has the maximum number of passengers!");
		
		for(Integer u: passenger)
			this.passengers.add(u);
	}

	/**
	 * Gets the ride id
	 * @return the ride id
	 */
	public int getRideID() 
	{
		return rideID;
	}

	/**
	 * Sets the ride id
	 * @param rideID the ride id
	 */
	public void setRideID(int rideID)
	{
		this.rideID = rideID;
	}

	/**
	 * Gets the person driving
	 * @return the driver
	 */
	public Integer getDriver() 
	{
		return driverID;
	}

	/**
	 * Sets the person driving
	 * @param driver
	 */
	public void setDriver(Integer driver)
	{
		this.driverID = driver;
	}

	/**
	 * Gets the departure time
	 * @return the departure time (time since epoch)
	 */
	public long getDeparture() {
		return departure;
	}

	/**
	 * Sets the departure time
	 * @param departure the departure time (time since epoch)
	 */
	public void setDeparture(long departure)
	{
		this.departure = departure;
	}

	/**
	 * Gets the location the ride is starting from
	 * @return The rides starting point
	 */
	public Location getSource() 
	{
		return src;
	}

	/**
	 * Sets the location the ride is starting from
	 * @param src the rides starting location
	 */
	public void setSource(Location src)
	{
		this.src = src;
	}

	/**
	 * Gets the destination
	 * @return the rides destination
	 */
	public Location getDestination()
	{
		return dest;
	}

	/**
	 * Sets the destination
	 * @param dest the rides destination
	 */
	public void setDestination(Location dest)
	{
		this.dest = dest;
	}
	
	/**
	 * Gets the total number of seats for this ride
	 * @return the total number of seats for this ride
	 */
	public int getNumberOfSeats()
	{
		return this.numSeats;
	}
	
	/**
	 * Sets the total number of seats for this ride
	 * @param num the total number of seats
	 */
	public void setNumberOfSeats(int num)
	{
		this.numSeats = num;
	}
	
	/**
	 * Gets the number of seats available
	 * @return the number of available seats
	 */
	public int getAvailableSeats()
	{
		return this.numSeats - this.passengers.size();
	}
}
